package b4.view
{
	import b4.model.SmartBirdExtension;
	import b4.model.vo.player.Obj;
	import b4.view.component.basic.PopMessage;
	import b4.view.component.events.ItemClickEvent;
	import b4.view.component.events.MsgEvent;
	import b4.view.component.smithy.BaseTab;
	import b4.view.component.smithy.Smithy;
	
	import flash.events.Event;
	
	import org.aswing.event.PopupEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class SmithyMediator extends Mediator implements IMediator
	{
		public static const  NAME:String = "SmithyMediator";
		public static const LOAD_ITEM_IMG:String = "loadItemImg";
		public static const SMITHY_COMMAND:String = "smithyCommand"; 
		public static const STRENGTHEN_EQUIP_BACK:String = "StrengthenEquipBack";
		public static const SHOW_STRENGHTEN_EQUIP_RESULT:String = "showStrengthenEquipResult";
		public static const SHOW_BREAK_EQUIP_RESULT:String = "showBreakEquip";
		public static const SHOW_REFINE_EQUIP_RESULT:String = "showRefineEquipBack";
	
		public function SmithyMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				SmithyMediator.LOAD_ITEM_IMG,
				SmithyMediator.STRENGTHEN_EQUIP_BACK,
				SmithyMediator.SHOW_STRENGHTEN_EQUIP_RESULT,
				SmithyMediator.SHOW_BREAK_EQUIP_RESULT,
				SmithyMediator.SHOW_REFINE_EQUIP_RESULT
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var result:Object = notification.getBody();
			var type:String = notification.getType();
			
			switch ( name )
			{
				case SmithyMediator.LOAD_ITEM_IMG:
					var temBox:BoxObj = smithy.loadItem(result as Obj);
					if (temBox != null)
					{
						var objList:Array = smithy.getLoadedItems();
						simpleClient.vo.me.pack.reFreshSmithyItem(objList);
						sendNotification(PackageDialogMediator.REFRESH_PACKAGE);
					}
					break; 
				case SmithyMediator.STRENGTHEN_EQUIP_BACK:
					
					var pop:PopMessage;
					if (result.res == 0)
					{
						pop = new PopMessage("装备强化成功！");
						sendNotification(SmithyMediator.SHOW_STRENGHTEN_EQUIP_RESULT);
					}
					else if (result.res == 100)
						pop = new PopMessage("物品不足！");
					else if (result.res == 101)
						pop = new PopMessage("装备不存在！");
					else if (result.res == 102)
						pop = new PopMessage("装备已经满级，无法继续强化！");
					else if (result.res == 103)
					{
						pop = new PopMessage("装备强化失败，强化等级-1");
						sendNotification(SmithyMediator.SHOW_STRENGHTEN_EQUIP_RESULT);
					}
					else if (result.res == 104)
					{
						pop = new PopMessage("装备强化失败，强化等级不变！");
						sendNotification(SmithyMediator.SHOW_STRENGHTEN_EQUIP_RESULT);
					}
					sendNotification(ApplicationMediator.ADD_CHILD, pop)
					break;
				case SmithyMediator.SHOW_BREAK_EQUIP_RESULT:
					var breakPop:PopMessage;
					if (result.rc == 100)
						breakPop = new PopMessage("凡品无法分解！");
					else if(result.rc == 101)
						breakPop = new PopMessage("装备不存在！");
					else if(result.rc == 0)
					{
						breakPop = new PopMessage("分解成功！");
						smithy.showBreakResult(simpleClient.vo.me.pack);
					}
					sendNotification(ApplicationMediator.ADD_CHILD, breakPop)
					break;
				case SmithyMediator.SHOW_REFINE_EQUIP_RESULT:
					var refinePop:PopMessage;
					if(result.rc == 0)
					{
						refinePop = new PopMessage("洗练成功！");
						smithy.showRefineResult(simpleClient.vo.me.pack);
					}
					else if(result.rc == 100)
						refinePop = new PopMessage("凡品无法洗练！");
					else if(result.rc == 101)
						refinePop = new PopMessage("装备不存在！");
					else if(result.rc == 102)
						refinePop = new PopMessage("洗练石不足！");
					sendNotification(ApplicationMediator.ADD_CHILD, refinePop)
					break;
				case SmithyMediator.SHOW_STRENGHTEN_EQUIP_RESULT:
					smithy.showStrengthenResult(simpleClient.vo.me.pack);
					simpleClient.vo.me.pack.reFreshSmithyItem(smithy.getLoadedItems());
					sendNotification(PackageDialogMediator.REFRESH_PACKAGE);
					break;
			}
		}
		
		override public function onRegister():void
		{
			smithy.show();
			smithy.addEventListener(PopupEvent.POPUP_CLOSED, onPopUpClosed);
			smithy.addEventListener(ItemClickEvent.BLACK_SMITHY_COMMAND, onButtonCommit);
			smithy.addEventListener(ItemClickEvent.ITEM_MOUSE_OVER, onItemMouseOver);
			smithy.addEventListener(ItemClickEvent.ITEM_MOUSE_OUT, onItemMouseOut);
			smithy.addEventListener(MsgEvent.SMITHY_TIP_MSG, onShowSmithyTipMsg);
			smithy.addEventListener(ItemClickEvent.LOAD_SMITHY_ITEM_OFF, onLoadOffSmithyItem);
		}
		
		private function onLoadOffSmithyItem(e:ItemClickEvent):void
		{
			var selPane:BaseTab = smithy.leftTabPane.getSelectedComponent() as BaseTab;
			simpleClient.vo.me.pack.reFreshSmithyItem(selPane.getLoadedItems());
			sendNotification(PackageDialogMediator.REFRESH_PACKAGE);
		}
		
		private function onShowSmithyTipMsg(e:MsgEvent):void
		{
			var data:Object = e.data;
			var tipPop:PopMessage  = new PopMessage(data.msg);
			sendNotification(ApplicationMediator.ADD_CHILD, tipPop)
		}

		private function onItemMouseOver(e:ItemClickEvent):void
		{
			facade.sendNotification(ApplicationMediator.SHOW_MOUSE_OVER_TIP, e.data);
		}
		
		private function onItemMouseOut(e:ItemClickEvent):void
		{
			facade.sendNotification(ApplicationMediator.HIDE_MOUSE_OVER_TIP);
		}
		
		private function onButtonCommit(e:ItemClickEvent):void
		{	
			simpleClient.blackSmithyOperation(e.data);
		}
		
		private function onPopUpClosed(event:Event):void
		{
			facade.removeMediator(NAME);
		}
		
		override public function onRemove():void
		{
			smithy.dispose();
		}
		
		private function get smithy():Smithy
		{
			return viewComponent as Smithy;
		}
		
		private function get simpleClient():SmartBirdExtension
		{
			return facade.retrieveProxy( SmartBirdExtension.NAME ) as SmartBirdExtension;
		}
	}
}